﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Project.Application;

namespace Project.AdminAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "ȨAPI", Version = "v1" });
            });

            //services.AddScoped<IAuthenticateService, AuthenticateService>();
            //读取配置文件
            //var conArray = Configuration.GetSection("AllowedHosts").Get<string>();
            //var token = Configuration.GetSection("TokenDto").Get<TokenDto>();
            //读取配置文件,并放入到类TokenDto中
            services.Configure<TokenDto>(Configuration.GetSection("TokenDto"));

            SetDepend("Project.Application", services);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapAreaControllerRoute(
                          name: "areas", "AuthorizationCenter",
                          pattern: "{area:exists}/{controller=User}/{action=Index}/{id?}");
            });
        }

        private void SetDepend(string assemblyName, IServiceCollection services)
        {
            if (!string.IsNullOrWhiteSpace(assemblyName))
            {
                Assembly assembly = Assembly.Load(assemblyName);
                List<Type> classList = assembly.GetTypes().Where(c => c.IsClass && c.FullName.Contains("AppService")).ToList();
                List<Type> interfaceList = assembly.GetTypes().Where(c => c.IsInterface && c.FullName.Contains("AppService")).ToList();

                if (classList.Count == interfaceList.Count)
                {
                    for (int i = 0; i < classList.Count; i++)
                    {
                        services.AddTransient(interfaceList[i], classList[i]);
                    }
                }
                else
                {
                    throw new Exception("依赖注入失败,请检测应用层接口和类代码以及命名规范");
                }
            }
        }
    }
}
